package com.orange.controller;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.handler.AuthFileCatalogMapService;
import com.orange.handler.AuthTestFileSchoolService;
import com.orange.modules.entity.AuthFileCatalogMap;
import com.orange.modules.entity.AuthTestFileSchoolMap;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/fileCatalogMap")
public class AuthFileCatalogMapController extends BaseController{
	
	private static final Logger logger = Logger.getLogger(AuthFileCatalogMapController.class);
	
	@Autowired
	private AuthFileCatalogMapService authFileCatalogMapService;
	
	@Autowired
	private AuthTestFileSchoolService authTestFileSchoolService;
	
	/**
	 * 根据目录ID查询该目录下的试题
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAuthFileByCatalogId" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAuthFileByCatalogId(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		String catalogId  = map.get("catalogId");
		String sid = map.get("sid");
		if(catalogId==null || StringUtils.isBlank(catalogId))
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"目录ID不能为空");
		if(sid!=null && StringUtils.isNotBlank(sid)){
			List<AuthTestFileSchoolMap> att = authTestFileSchoolService.queryTestFile(sid, "", catalogId);
			if(att!=null &&  att.size()>0)
				return toResponse(att,ReturnCode.FAIL,"此类型下已经抽取过试题!");
		}
		List<AuthFileCatalogMap> list = authFileCatalogMapService.queryFileByCatalogId(catalogId);
		return toResponse(list,ReturnCode.SUCCESS,"查询成功");
	}
}
